package com.ssm.sfzy.controller;

import com.ssm.sfzy.constant.CommonConstant;
import com.ssm.sfzy.entity.Announcement;
import com.ssm.sfzy.service.AnnouncementService;
import com.ssm.sfzy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @ClassName AnnouncementController
 * @Author xiaoliu
 * @Date 2024年03月30日 17:39
 */

@RestController
@CrossOrigin
@RequestMapping("/announcement")
public class AnnouncementController {
    @Autowired
    private AnnouncementService announcementService;

    @GetMapping("/find")
    public Object find(
            @RequestParam(defaultValue = CommonConstant.PAGE_NUM_DEFAULT) Integer pageNum,
            @RequestParam(defaultValue = CommonConstant.PAGE_SIZE_DEFAULT) Integer pageSize,
            Announcement announcement
    ){
        Map<String, Object> map = announcementService.findPage(pageNum, pageSize, announcement);
        return  Result.success(map);
    }


    @GetMapping("/findById/{id}")
    public Object findById(@PathVariable Integer id){
        Announcement announcement = announcementService.findById(id);
        return Result.success(announcement);
    }


    @PostMapping("/add")
    public Object add(@RequestBody Announcement announcement) throws Exception {
        announcementService.add(announcement);
        return Result.success();
    }

    @PutMapping("/edit")
    public Object edit(@RequestBody Announcement announcement) throws Exception {
        announcementService.edit(announcement);
        return Result.success();
    }
    @DeleteMapping("/remove/{id}")
    public Object remove(@PathVariable Integer id){
        announcementService.remove(id);
        return Result.success();
    }

    @DeleteMapping("/removeBatch/{ids}")
    public Object removeBatch(Integer[] ids){
        announcementService.removeBatch(ids);
        return Result.success();
    }
}
